package com.firefly.service.query.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.ums.mapper.AdminMapper;
import com.firefly.dao.dao.ums.po.AdminPo;
import com.firefly.dao.dao.ums.service.AdminService;
import com.firefly.dao.assembler.ums.AdminAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.ums.AdminSearchDto;
import com.firefly.dao.vo.ums.AdminListVo;
import com.firefly.dao.vo.ums.AdminNameListVo;
import com.firefly.dao.vo.ums.AdminNameVo;
import com.firefly.dao.vo.ums.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 管理员信息查询
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/9
 **/
@Service
public class AdminQuery {
    private final AdminService adminService;
    private final AdminMapper adminMapper;
    private final AdminAssembler adminAssembler;
    @Autowired
    public AdminQuery(AdminService adminService,AdminMapper adminMapper,AdminAssembler adminAssembler){
        this.adminService = adminService;
        this.adminMapper = adminMapper;
        this.adminAssembler = adminAssembler;
    }
    // 获取列表，mapper自定义查询关联
    public AdminListVo getListAndRoleDepartment(AdminSearchDto request) throws Exception {
        List<AdminVo> list = adminMapper.getListAndRoleDepartment(request);
        QueryWrapper<AdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!request.key.isEmpty(), "name", request.key).select("id");
        Long count = adminService.count(queryWrapper);
        AdminListVo result = new AdminListVo();
        result.setCount(count);
        result.setList(list);
        return result;
    }
    // 获取列表，mapper自定义查询关联
    public AdminNameListVo getList(AdminSearchDto request) throws Exception {
        QueryWrapper<AdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!request.key.isEmpty(), "name", request.key);
        Long count = adminService.count(queryWrapper);
        queryWrapper.last("limit " + request.offset + "," + request.pageSize).select("id","name","phone","realname");
        List<AdminPo> all = adminService.list(queryWrapper);
        List<AdminNameVo> list = adminAssembler.getNameList(all);
        AdminNameListVo result = new AdminNameListVo();
        result.setCount(count);
        result.setList(list);
        return result;
    }
    //	获取单条，返回字段控制
    public AdminVo getDetail(CommonDto request) {
        return adminMapper.getDetailAndRoleDepartmentById(request.detailId);
    }
}
